package com.swjd.sell.contorller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.swjd.sell.dto.OrderDTO;

import com.swjd.sell.exception.SellException;
import com.swjd.sell.pojo.OrderDetail;
import com.swjd.sell.pojo.enums.ResultEnum;
import com.swjd.sell.service.OrderService;
import com.swjd.sell.utils.ResultVOUtil;
import com.swjd.sell.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author 刘涛
 * @create 2021/8/30 23:05
 */
@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 创建订单
     */
    @PostMapping("/create")
    public ResultVO createOrder(@RequestParam("name") String buyerName, @RequestParam("phone") String buyerPhone, @RequestParam("address") String buyerAddress, @RequestParam("openid") String buyerOpenid, @RequestParam("items") String stockDTD) {
        OrderDTO orderDTO = new OrderDTO();
        orderDTO.setBuyerName(buyerName);
        orderDTO.setBuyerPhone(buyerPhone);
        orderDTO.setBuyerAddress(buyerAddress);
        orderDTO.setBuyerOpenid(buyerOpenid);
        //把JSON转成List
        try {
            //使用fastjson库进行转换
            List<OrderDetail> orderDetails = JSONObject.parseArray(stockDTD, OrderDetail.class);
            orderDTO.setOrderDetails(orderDetails);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("【对象转换】 错误, string={}", stockDTD);
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        if (CollectionUtils.isEmpty(orderDTO.getOrderDetails())) {
            log.error("【创建订单】 购物车不能为空");
            throw new SellException(ResultEnum.CART_EMPTY);
        }
        OrderDTO orderDTO1 = orderService.create(orderDTO);

        return ResultVOUtil.success(orderDTO1.getOrderId());
    }

    /**
     * 订单列表
     */
    @GetMapping("/list")
    public ResultVO orderList(@RequestParam("openid") String buyerOpenid, @RequestParam(value = "page", defaultValue = "0") Long current, @RequestParam(value = "size", defaultValue = "10") Long size) {
        if (buyerOpenid.isEmpty()) {
            log.error("【查询订单列表】 openid为空");
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        Page<Object> Page = new Page<>(current, size);
        IPage<OrderDTO> orderDTOIPage = orderService.selectByBuyerOpenId(buyerOpenid, Page);
        return ResultVOUtil.success(orderDTOIPage.getRecords());
    }

    /**
     * 订单详情展示
     */
    @GetMapping("/detail")
    public ResultVO orderDerail(@RequestParam("openid") String buyerOpenid, @RequestParam("orderId") String orderId) {
        OrderDTO orderDTO = orderService.selectById(orderId);
        if (orderDTO == null) {
            return null;
        } else if (!orderDTO.getBuyerOpenid().equals(buyerOpenid)) {
            log.error("【查询订单】 订单的openid不一致 openId={}", orderDTO.getBuyerOpenid());
            throw new SellException(ResultEnum.ORDER_OWNER_ERROR);
        }
        return ResultVOUtil.success(orderDTO);
    }

    /**
     * 取消订单
     */
    @PostMapping("/cancel")
    public ResultVO cancelOrder(@RequestParam("openid") String buyerOpenid, @RequestParam("orderId") String orderId) {
        OrderDTO orderDTO = orderService.selectById(orderId);
        if (orderDTO == null) {
            return null;
        } else if (!orderDTO.getBuyerOpenid().equals(buyerOpenid)) {
            log.error("【查询订单】 订单的openid不一致 openId={}", orderDTO.getBuyerOpenid());
            throw new SellException(ResultEnum.ORDER_OWNER_ERROR);
        }
        OrderDTO orderDTO1 = orderService.cancelOrder(orderDTO);
        return ResultVOUtil.success();
    }

}
